package com.pattern.demo.protogenesis;

import java.util.ArrayList;

/**
 * @ClassName: WeatherData  气象台数据
 * @Description:
 * @Date: 2020/8/27 14:54
 * @Author: Song JinGen
 */
public class WeatherData implements Subject {

    /**
     * 观察者数组
     */
    private ArrayList observers;

    /**
     *  温度
     */
    private float temperature;

    /**
     * 湿度
     */
    private float humidity;

    /**
     *  压力
     */
    private float pressure;

    /**
     * 描述
     */
    private String msg;

    /**
     * 风力
     */
    private String wind;

    public WeatherData(){
        observers = new ArrayList();
    }

    /**
     * 注册时候把观察者放在观察者集合中存储
     * @param observer
     */
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    /**
     * 从观察者集合中删除当前观察者
     * @param observer
     */
    public void removeObserver(Observer observer) {
        int i = observers.indexOf(observer);
        if (i >= 0) {
            observers.remove(i);
        }
    }

    /**
     * 获取所有观察者，并且通知修改数据
     */
    public void notifyObserver() {
        for (int i = 0; i < observers.size(); i++) {
            Observer o = (Observer) observers.get(i);
            o.update(temperature,humidity,pressure,wind,msg);
        }
    }

    /**
     * 数据变动，通知所有观察者
     */
    public void measurementsChanged(){
        notifyObserver();
    }

    /**
     * 为了方便测试使用，手动更改值的变化，观察消费者是否收到值的变化
     * @param temperature
     * @param humidity
     * @param pressure
     */
    public void setMeasurements(float temperature, float humidity, float pressure, String wind, String msg){
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        this.wind = wind;
        this.msg = msg;
        measurementsChanged();
    }
}
